package com.leo.result;

import com.leo.common.ReturnCode;
import com.leo.common.ReturnCodeEnum;

/**
 * @author liusl
 * @date 2019/3/7-9:56
 */
public class ApiResult<T> extends AbstractResultSupport {

    protected T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 接口调用失败,有错误字符串码和描述,有返回对象
     *
     * @param code
     * @param message
     * @param data
     * @param <U>
     * @return
     */
    public static <U> ApiResult<U> failure(int code, String message, U data) {
        return newResult(code, message, data);
    }

    /**
     * 接口调用失败,有错误字符串码和描述,没有返回对象
     *
     * @param code
     * @param message
     * @param <U>
     * @return
     */
    public static <U> ApiResult<U> failure(int code, String message) {
        return failure(code, message, null);
    }

    public static <U> ApiResult<U> failure(ReturnCode returnCode) {
        return newResult(returnCode, null);
    }
    public static <U> ApiResult<U> success(){
        return newResult(ReturnCodeEnum.SUCCESS, null);
    }
    public static <U> ApiResult<U> success(U data) {
        return newResult(ReturnCodeEnum.SUCCESS, data);
    }

    public static <U> ApiResult<U> success(int code, String desc, U data) {
        return newResult(code, desc, data);
    }


    private static <U> ApiResult<U> newResult(ReturnCode returnCode, U data) {
        ApiResult<U> apiResult = new ApiResult<U>();
        apiResult.setCode(returnCode.getReturnCode());
        apiResult.setDesc(returnCode.getReturnDesc());
        apiResult.setData(data);
        return apiResult;
    }

    private static <U> ApiResult<U> newResult(int code, String desc, U data) {
        ApiResult<U> apiResult = new ApiResult<U>();
        apiResult.setCode(code);
        apiResult.setDesc(desc);
        apiResult.setData(data);
        return apiResult;
    }
}
